package org.keyroy.map_editor.map.layers;

import it.sauronsoftware.base64.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import org.keyroy.map_editor.map.Map;
import org.keyroy.map_editor.map.m.MapRect;
import org.keyroy.tag.Tag;
import org.keyroy.tag.XmlSupport;

public class FloorLayer implements XmlSupport {
	private int maxID;
	public int[][] data;

	public FloorLayer() {

	}

	public void setMap(Map map) {
		data = new int[map.getColumns()][map.getRows()];
	}

	public final void setFloor(int id, MapRect rect) {
		try {
			for (int i = rect.x; i < rect.getRight(); i++) {
				for (int j = rect.y; j < rect.getBottom(); j++) {
					data[i][j] = id;
				}
			}
			maxID = Math.max(maxID, id);
			// printData();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public final void deleteFloor(int id) {
		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[i].length; j++) {
				if (data[i][j] == id) {
					data[i][j] = 0;
				}
			}
		}
	}

	@Override
	public String getTagName() {
		return "fl";
	}

	@Override
	public void initObj(Tag tag) {
		String dataString = tag.getParameterValue("d");
		if (dataString != null) {
			maxID = tag.getParameterIntValue("mid");
			byte[] bs = Base64.decode(dataString.getBytes());
			try {
				ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(bs);
				DataInputStream dataInputStream = new DataInputStream(arrayInputStream);
				int width = data.length;
				int height = data[0].length;
				if (maxID < Byte.MAX_VALUE) {
					for (int j = 0; j < height; j++) {
						for (int i = 0; i < width; i++) {
							data[i][j] = dataInputStream.read();
						}
					}
				} else if (maxID < Short.MAX_VALUE) {
					for (int j = 0; j < height; j++) {
						for (int i = 0; i < width; i++) {
							data[i][j] = dataInputStream.readShort();
						}
					}

				} else {
					for (int j = 0; j < height; j++) {
						for (int i = 0; i < width; i++) {
							data[i][j] = dataInputStream.readInt();
						}
					}
				}
				dataInputStream.close();
				arrayInputStream.close();
			} catch (Exception e) {
			}

		}
	}

	@Override
	public void initTag(Tag tag) {
		if (data != null) {
			tag.addParameter("mid", maxID);
			try {
				int width = data.length;
				int height = data[0].length;
				ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(width * height);
				DataOutputStream dataOutputStream = new DataOutputStream(arrayOutputStream);
				if (maxID < Byte.MAX_VALUE) {
					for (int j = 0; j < height; j++) {
						for (int i = 0; i < width; i++) {
							dataOutputStream.write((byte) data[i][j]);
						}
					}

				} else if (maxID < Short.MAX_VALUE) {
					for (int j = 0; j < height; j++) {
						for (int i = 0; i < width; i++) {
							dataOutputStream.writeShort(data[i][j]);
						}
					}
				} else {
					for (int j = 0; j < height; j++) {
						for (int i = 0; i < width; i++) {
							dataOutputStream.writeInt(data[i][j]);
						}
					}
				}
				String dataString = new String(Base64.encode(arrayOutputStream.toByteArray()));
				tag.addParameter("d", dataString);
				dataOutputStream.close();
				arrayOutputStream.close();
			} catch (Exception e) {
			}
		}
	}
}
